/*
 *    Copyright 2009-2023 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       https://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.ryan.builder;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.ryan.Tools;
import org.apache.ibatis.session.Configuration;

/**
 * 生产inster sql
 */
public class InsertBuilder<E> {
  private SQL sql = new SQL();
  private E bean;
  /**
   * 参数列表
   */
  private Map<String, Object> params = new HashMap<>();

  /**
   * 将需要插入的对象传出 init().optSql().getSql()
   *
   * @param bean
   */
  public InsertBuilder(E bean) {
    this.bean = bean;
  }

  /**
   * 创建Builder并初始化
   *
   * @param <T>
   * @param bean
   *
   * @return
   */
  public static <T> InsertBuilder<T> build(T bean) {
    InsertBuilder<T> builder = new InsertBuilder<>(bean);
    Tools.setIdIfNull(bean);
    builder.init();
    return builder;
  }

  /**
   * 给数据创建时间和修改时间列赋值
   */
  private void setTimeIfNull() {
    if (StringUtils.isNotBlank(Configuration.createTimeColunm)) {
      this.values(Tools.toUnderLine(Configuration.createTimeColunm), new Date());
    }

    if (StringUtils.isNotBlank(Configuration.updateTimeColunm)) {
      this.values(Tools.toUnderLine(Configuration.updateTimeColunm), new Date());
    }
  }

  /**
   * 获取最后组装的SQL
   *
   * @return
   */
  public String getSql() {
    return this.sql.toString();
  }

  /**
   * 根据bean生成 创建SQL的 参数对象
   *
   * @return
   */
  public InsertBuilder<E> allPropSql() {
    List<Field> fields = Tools.getInstanceFields(this.bean.getClass());
    for (int i = 0; i < fields.size(); ++i) {
      Field field = (Field) fields.get(i);
      field.setAccessible(true);
      String name = field.getName();
      Object value = Tools.getProperty(this.bean, name, (String) null);
      this.values(name, value);
    }

    this.setTimeIfNull();
    return this;
  }

  /**
   * 根据bean生成 创建SQL的 参数对象 只有值不为空才插入
   *
   * @return
   */
  public InsertBuilder<E> optSql() {
    List<Field> fields = Tools.getInstanceFields(this.bean.getClass());
    for (int i = 0; i < fields.size(); ++i) {
      Field field = (Field) fields.get(i);
      field.setAccessible(true);
      String name = field.getName();
      Object value = Tools.getProperty(this.bean, name, (String) null);
      if (value != null) {
        this.values(name, value);
      }
    }
    this.setTimeIfNull();
    return this;
  }

  /**
   * 初始化设置表名
   */
  private void init() {
    sql.INSERT_INTO(Tools.tableName(this.bean.getClass()));
  }

  public void values(String propName, Object value) {
    // param2 根据 SProviderc.class 的方法来的 是第二个参数
    String paramName = String.format("#{param2.%s}", propName);
    sql.VALUES(Tools.toUnderLine(propName), paramName);
    params.put(propName, value);
  }

  public Map<String, Object> getParams() {
    return this.params;
  }
}
